﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Sale_Model;
using Sale_Common;
using Sale_DAL;
namespace Sale_Operation
{
    public class WorkPlanOp : Component
    {
        public override bool Save(object obj)
        {
            bool blRes = false;
            WorkPlan SModel = obj as   WorkPlan;
            try
            {
                StringBuilder sbSql = new StringBuilder();
                if (SModel.WPlan_Id == 0)
                {
                    sbSql.Append(" INSERT INTO WorkPlan (WPlan_Title,WPlan_Content,WPlan_StartTime,WPlan_EndTime,WPlan_Number,WPlan_Unit,WPlan_WarningTime,WPlan_Enable,WPlan_Man,WPlan_State,State,CreateTime,CreateMan) SELECT ");
                    sbSql.Append("WPlan_Title='" + ValueHandler.GetStringValue(SModel.WPlan_Title, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("WPlan_Content='" + ValueHandler.GetStringValue(SModel.WPlan_Content, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("WPlan_StartTime='" + ValueHandler.GetStringValue(SModel.WPlan_StartTime, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("WPlan_EndTime='" + ValueHandler.GetStringValue(SModel.WPlan_EndTime, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("WPlan_Number='" + ValueHandler.GetStringValue(SModel.WPlan_Number, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("WPlan_Unit='" + ValueHandler.GetStringValue(SModel.WPlan_Unit, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("WPlan_WarningTime='" + ValueHandler.GetStringValue(SModel.WPlan_WarningTime, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("WPlan_Enable='" + ValueHandler.GetStringValue(SModel.WPlan_Enable, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("WPlan_Man='" + ValueHandler.GetStringValue(SModel.WPlan_Man, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("WPlan_State='" + ValueHandler.GetStringValue(SModel.WPlan_State, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("State=1,");
                    sbSql.Append("CreateTime=getdate(),");
                    sbSql.Append("CreateMan=" + LinkService.GetCurrentUser().SUser_Id);
                    db.Open();
                    SqlHelper.ExecuteNonQuery(db.Transaction, CommandType.Text, sbSql.ToString());
                    SModel.WPlan_Id = ValueHandler.GetIntNumberValue(SqlHelper.ExecuteScalar(db.Transaction, CommandType.Text, "select @@identity "));
                }
                else
                {
                    sbSql.Append(" UPDATE WorkPlan SET ");
                    sbSql.Append("WPlan_Title='" + ValueHandler.GetStringValue(SModel.WPlan_Title, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("WPlan_Content='" + ValueHandler.GetStringValue(SModel.WPlan_Content, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("WPlan_StartTime='" + ValueHandler.GetStringValue(SModel.WPlan_StartTime, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("WPlan_EndTime='" + ValueHandler.GetStringValue(SModel.WPlan_EndTime, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("WPlan_Number='" + ValueHandler.GetStringValue(SModel.WPlan_Number, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("WPlan_Unit='" + ValueHandler.GetStringValue(SModel.WPlan_Unit, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("WPlan_WarningTime='" + ValueHandler.GetStringValue(SModel.WPlan_WarningTime, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("WPlan_Enable='" + ValueHandler.GetStringValue(SModel.WPlan_Enable, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("WPlan_Man='" + ValueHandler.GetStringValue(SModel.WPlan_Man, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("WPlan_State='" + ValueHandler.GetStringValue(SModel.WPlan_State, ValueHandler.CharHandlerType.IsRepChar) + "' ");
                    sbSql.Append("WHERE WPlan_Id=" + SModel.WPlan_Id);
                    db.Open();
                    SqlHelper.ExecuteNonQuery(db.Transaction, CommandType.Text, sbSql.ToString());                 
                }
                db.Transaction.Commit();
                blRes = true;
            }
            catch (Exception ex)
            {
                db.Transaction.Rollback();
                LinkService.InsertLog(ESysLogType.Error, "新增日程安排出错！"+ex.ToString(), EMenuList.日程安排);
            }
            db.Close();
            return blRes;


            
        }

        public override bool HasExists(object obj)
        {
            throw new NotImplementedException();
        }

        public override bool Delete(object obj)
        {
            WorkPlan SM = obj as WorkPlan;
            bool blRes = false;
            try
            {
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append("UPDATE WorkPlan SET ");
                sbSql.Append("State=0");
                sbSql.Append("WHERE WPlan_Id=" + SM.WPlan_Id);
                SqlHelper.ExecuteNonQuery(db.Connection, CommandType.Text, sbSql.ToString());
                blRes = true;
                LinkService.InsertLog(ESysLogType.Operation, "删除日程安排信息[" + ValueHandler.GetStringValue(SM.WPlan_Title, ValueHandler.CharHandlerType.IsRepChar) + "]！", EMenuList.日程安排);
            }
            catch (Exception ex)
            {
                LinkService.InsertLog(ESysLogType.Error, "删除日程安排信息出错！[" + ex.ToString() + "]", EMenuList.日程安排);
            }
            db.Close();
            return blRes;
        }

        public override DataTable GetList(string strCondition, int iCurrentPageIndex, ref int iRecordCount)
        {
            DataBase db = new DataBase();
            try
            {
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append(@" SELECT WPlan_Id,WPlan_Title,WPlan_Content,WPlan_StartTime,WPlan_EndTime,WPlan_Number,cast(WPlan_Number as nvarchar(10))+UnitName AS WPlan_WarningInfo,WPlan_Unit,WPlan_WarningTime,WPlan_Enable,WPlan_Man,WPlan_State,A.State,A.CreateTime,A.CreateMan,SUser_Name FROM WorkPlan A");
                sbSql.Append(" LEFT JOIN SysUser B ON (A.WPlan_Man=B.SUser_Id)");
                sbSql.Append(" LEFT JOIN View_WorkPlanUnit C ON (C.UnitId=A.WPlan_Unit)");
                sbSql.Append(" WHERE A.State=1 ");
                if (strCondition != "")
                {
                    sbSql.Append(" AND " + strCondition);
                }
                sbSql.Append(" ORDER BY A.CreateTime DESC ");
                db.Data = SqlHelper.FillDataSet(db.Connection, sbSql.ToString(), iCurrentPageIndex, ref iRecordCount, LinkService.PageSize);
            }
            catch (Exception ex)
            {
                LinkService.InsertLog(ESysLogType.Error, "[日程安排获取出错]:" + ex.ToString(), EMenuList.日程安排);
            }
            db.Close();
            return db.Data.Tables[0];
        }
        public override DataTable GetList(string strCondition)
        {
            DataBase db = new DataBase();
            try
            {
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append(@" SELECT WPlan_Id,WPlan_Title,WPlan_Content,WPlan_StartTime,WPlan_EndTime,WPlan_Number,cast(WPlan_Number as nvarchar(10))+UnitName AS WPlan_WarningInfo,WPlan_Unit,WPlan_WarningTime,WPlan_Enable,WPlan_Man,WPlan_State,A.State,A.CreateTime,A.CreateMan,SUser_Name FROM WorkPlan A");
                sbSql.Append(" LEFT JOIN SysUser B ON (A.WPlan_Man=B.SUser_Id)");
                sbSql.Append(" LEFT JOIN View_WorkPlanUnit C ON (C.UnitId=A.WPlan_Unit)");
                sbSql.Append(" WHERE A.State=1 ");
                if (strCondition != "")
                {
                    sbSql.Append(" AND " + strCondition);
                }
                sbSql.Append(" ORDER BY A.CreateTime DESC ");
                SqlHelper.FillDataset(db.Connection, CommandType.Text, sbSql.ToString(), db.Data, new string[] { "dtList"});
            }
            catch (Exception ex)
            {
                LinkService.InsertLog(ESysLogType.Error, "[日程安排获取出错]:" + ex.ToString(), EMenuList.日程安排);
            }
            db.Close();
            return db.Data.Tables[0];
        }
        public  DataTable GetList(int iTop , string strCondition)
        {
            DataBase db = new DataBase();
            try
            {
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append(@" SELECT TOP  "+iTop+" WPlan_Id,WPlan_Title,WPlan_Content,WPlan_StartTime,WPlan_EndTime,WPlan_Number,cast(WPlan_Number as nvarchar(10))+UnitName AS WPlan_WarningInfo,WPlan_Unit,WPlan_WarningTime,WPlan_Enable,WPlan_Man,WPlan_State,A.State,A.CreateTime,A.CreateMan,SUser_Name FROM WorkPlan A");
                sbSql.Append(" LEFT JOIN SysUser B ON (A.WPlan_Man=B.SUser_Id)");
                sbSql.Append(" LEFT JOIN View_WorkPlanUnit C ON (C.UnitId=A.WPlan_Unit)");
                sbSql.Append(" WHERE A.State=1 ");
                if (strCondition != "")
                {
                    sbSql.Append(" AND " + strCondition);
                }
                sbSql.Append(" ORDER BY A.CreateTime DESC ");
                SqlHelper.FillDataset(db.Connection, CommandType.Text, sbSql.ToString(), db.Data, new string[] { "dtList" });
            }
            catch (Exception ex)
            {
                LinkService.InsertLog(ESysLogType.Error, "[日程安排获取出错]:" + ex.ToString(), EMenuList.日程安排);
            }
            db.Close();
            return db.Data.Tables[0];
        }

        public override object GetModel(object obj)
        {
            db = new DataBase();
            try
            {
                WorkPlan wp = obj as WorkPlan;
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append(@" SELECT WPlan_Id,WPlan_Title,WPlan_Content,WPlan_StartTime,WPlan_EndTime,WPlan_Number,cast(WPlan_Number as nvarchar(10))+UnitName AS WPlan_WarningInfo,WPlan_Unit,WPlan_WarningTime,WPlan_Enable,WPlan_Man,WPlan_State,A.State,A.CreateTime,A.CreateMan,SUser_Name FROM WorkPlan A");
                sbSql.Append(" LEFT JOIN SysUser B ON (A.WPlan_Man=B.SUser_Id)");
                sbSql.Append(" LEFT JOIN View_WorkPlanUnit C ON (C.UnitId=A.WPlan_Unit)");
                sbSql.Append(" WHERE WPlan_Id=" + wp.WPlan_Id);
                SqlHelper.FillDataset(db.Connection, CommandType.Text, sbSql.ToString(), db.Data, new string[] { "dtList"});
                if (db.Data.Tables[0].Rows.Count > 0)
                {
                    DataRow dr = db.Data.Tables[0].Rows[0];
                    wp.CreateMan = ValueHandler.GetIntNumberValue(dr["CreateMan"]);
                    wp.CreateTime = DateTime.Parse(dr["CreateTime"].ToString());
                    wp.State = ValueHandler.GetIntNumberValue(dr["State"]);
                    wp.WPlan_Content = ValueHandler.GetStringValue(dr["WPlan_Content"]);
                    wp.WPlan_Enable = ValueHandler.GetIntNumberValue(dr["WPlan_Enable"]);
                    wp.WPlan_EndTime = DateTime.Parse(dr["WPlan_EndTime"].ToString());
                    wp.WPlan_Man = ValueHandler.GetIntNumberValue(dr["WPlan_Man"]);
                    wp.WPlan_Number = ValueHandler.GetIntNumberValue(dr["WPlan_Number"]);
                    wp.WPlan_StartTime = DateTime.Parse(dr["WPlan_StartTime"].ToString());
                    wp.WPlan_State = ValueHandler.GetIntNumberValue(dr["WPlan_State"]);
                    wp.WPlan_Title = ValueHandler.GetStringValue(dr["WPlan_Title"]);
                    wp.WPlan_Unit = ValueHandler.GetIntNumberValue(dr["WPlan_Unit"]);
                    wp.WPlan_WarningTime = DateTime.Parse(dr["WPlan_WarningTime"].ToString());
                }
                
            }
            catch (Exception ex)
            {
                LinkService.InsertLog(ESysLogType.Error, "[日程安排获取出错]:" + ex.ToString(), EMenuList.日程安排);
            }
            db.Close();
            return obj;
        }
        public DataTable GetView_WorkPlanUnit()
        {
            db = new DataBase();
            try
            {
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append(@" SELECT UnitId,UnitName FROM  View_WorkPlanUnit");
                SqlHelper.FillDataset(db.Connection, CommandType.Text, sbSql.ToString(), db.Data, new string[] { "dtList"});
            }
            catch (Exception ex)
            { }
            return db.Data.Tables[0]; 
        }
         
    }
}
